package pro.spring.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import pro.spring.inter.UsersMapper;
import pro.spring.inter.UdiscussMapper;
import pro.spring.inter.UdiscusserMapper;
import pro.spring.model.Udiscuss;
import pro.spring.model.Udiscusser;
import pro.spring.model.Users;
import pro.spring.vo.JsonVo;
import pro.spring.vo.PaginationVo;
import pro.spring.inter.UsupportMapper;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class UdiscusserService{

	@Autowired
	private UdiscusserMapper UdiscusserMapper;
	
	@Autowired
	private UdiscussMapper UdiscussMapper;
	
	@Autowired
	private UsupportMapper UsupportMapper;
	
	@Autowired
	private UsersMapper UsersMapper;
	
	public int deleteByPrimaryKey(Integer id) {
		return UdiscusserMapper.deleteByPrimaryKey(id);
	}

	public int insert(Udiscusser record) {
		record.setDrContent(record.getDrContent().replace("contenteditable=\"true\"", " "));
		return UdiscusserMapper.insert(record);
	}
	
	public int selectNewId(){
		return UdiscusserMapper.selectNewId();
	}
	
	public Integer selectCountBydrId(Integer id){
		return UdiscussMapper.selectCountBydrId(id);
	}

	//包含udiscuss
	public Udiscusser selectByPrimaryKeys(Integer uId,Integer id,Integer page) {
		Udiscusser u = UdiscusserMapper.selectByPrimaryKey(id);
		System.out.println(page+"aaaaaaaacai"+UdiscussMapper.selectCountBydrId(id));
		Integer count = UdiscussMapper.selectCountBydrId(id);
		Integer start =0;
		if(count-page <=0){
			start = 0;
		}else{
			start=count-page;
		}
		System.out.println(count+"ssssssssssss"+start);
		List<Udiscuss> us = UdiscussMapper.selectBydrId(id,start,page);
		for(Udiscuss ur:us){
			ur.setDsContent(ur.getDsContent());
			ur.setUsers(UsersMapper.selectByPrimaryKey(ur.getUsersUId()));
			ur.setSuport(UsupportMapper.selectCountBydsId(ur.getDsId()));
			int aa = 1 ;
			if(uId!=null){
				if(UsupportMapper.selectZan(uId,ur.getDsId())==null){
					aa = 0;
				}
			}
			ur.setSc(aa);
		}
		u.setDiscuss(us);
		return u;
	}
	
	public Udiscusser selectByPrimaryKey(Integer id) {
		return UdiscusserMapper.selectByPrimaryKey(id);
	}
	
	public Udiscusser selectByPrimaryKeyXianxia(Integer id) {
		Udiscusser u = UdiscusserMapper.selectByPrimaryKey(id);
		u.setAnswers(UdiscussMapper.selectCountBydrId(u.getDrId()));
		u.setUsers(UsersMapper.selectByPrimaryKey(u.getUsersUId()));
		List<Users> li = new ArrayList<Users>();
		List<Udiscuss> list = UdiscussMapper.selectOneXianUser(u.getDrId());
		for(int i=0;i<list.size();i++){
			li.add(UsersMapper.selectByPrimaryKey(list.get(i).getUsersUId()));
		}
		u.setUli(li);
		return u;
	}

	public int updateByPrimaryKey(Udiscusser record) {
		return UdiscusserMapper.updateByPrimaryKey(record);
	}
	
	
	//discusser分页
	public PaginationVo<Udiscusser> getPaginationInfo(String key,int pagenum,int pagesize){
		//int pagenum是当前的页码,int pagesize是每页显示的数据数量
		PaginationVo<Udiscusser> pageVo=new PaginationVo<Udiscusser>();
		//一定要在setPageCount()方法之后执行setPageNum
		List<Udiscusser> list = null;
		if(key ==null || key == ""){
			pageVo.setCount(UdiscusserMapper.selectCount());
			pageVo.setRows(pagesize);
			pageVo.setPageNum(pagenum);
			list = UdiscusserMapper.selectListPage(pageVo.getOffset(), pageVo.getRows());
		}else{
			pageVo.setCount(UdiscusserMapper.selectCountByKey(key));
			pageVo.setRows(pagesize);
			pageVo.setPageNum(pagenum);
			list = UdiscusserMapper.selectByKey(key,pageVo.getOffset(), pageVo.getRows());
		}
		for(Udiscusser one:list){
			one.setAnswers(UdiscussMapper.selectCountBydrId(one.getDrId()));
			one.setUsers(UsersMapper.selectByPrimaryKey(one.getUsersUId()));
		}
		pageVo.setList(list);
		return pageVo;
	}
	
	//线下交流 前台
	public PaginationVo<Udiscusser> getXianxia(int pagenum,int pagesize){
		//int pagenum是当前的页码,int pagesize是每页显示的数据数量
		PaginationVo<Udiscusser> pageVo=new PaginationVo<Udiscusser>();
		//一定要在setPageCount()方法之后执行setPageNum
		pageVo.setCount(UdiscusserMapper.selectCountByXianxiaa());
		pageVo.setRows(pagesize);
		pageVo.setPageNum(pagenum);
		List<Udiscusser> list = UdiscusserMapper.selectXianxia(pageVo.getOffset(), pageVo.getRows());
		for(Udiscusser one:list){
			one.setAnswers(UdiscussMapper.selectCountBydrId(one.getDrId()));
			one.setUsers(UsersMapper.selectByPrimaryKey(one.getUsersUId()));
			String m = one.getDrContent();
			String n = m.substring(m.indexOf("标题")+3,m.indexOf("</p>"));
			one.setTitle(n);
			one.setDrContent(n);
		}
		pageVo.setList(list);
		return pageVo;
	}
	
	//discusser分页 by one user
	public PaginationVo<Udiscusser> getPageByUid(int uid,int pagenum,int pagesize){
		//int pagenum是当前的页码,int pagesize是每页显示的数据数量
		PaginationVo<Udiscusser> pageVo=new PaginationVo<Udiscusser>();
		//一定要在setPageCount()方法之后执行setPageNum
		pageVo.setCount(UdiscusserMapper.selectCountByUid(uid));
		pageVo.setRows(pagesize);
		pageVo.setPageNum(pagenum);
		List<Udiscusser> list = UdiscusserMapper.selectByUid(uid,pageVo.getOffset(), pageVo.getRows());
		for(Udiscusser one:list){
			one.setAnswers(UdiscussMapper.selectCountBydrId(one.getDrId()));
			one.setUsers(UsersMapper.selectByPrimaryKey(one.getUsersUId()));
			String n = getNOHTML(one.getDrContent());
			if(n.length()>200){
				n = n.substring(0, 100)+"..." ;
			}
			one.setDrContent(n);
		}
		pageVo.setList(list);
		return pageVo;
	}
	
	//去除标签
	public String getNOHTML(String html){
		String htmlStr =html;
		String textStr =""; 
		Pattern p_script; 
		Matcher m_script; 
		Pattern p_style; 
		Matcher m_style; 
		Pattern p_html; 
		Matcher m_html;
		Pattern p_kong; 
		Matcher m_kong; 
		try { 
			String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>"; //定义script的正则表达式{或<script[^>]*?>[\\s\\S]*?<\\/script> } 
			String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>"; //定义style的正则表达式{或<style[^>]*?>[\\s\\S]*?<\\/style> } 
			String regEx_html = "<[^>]+>"; //定义HTML标签的正则表达式 
			String kong="&nbsp;";
			
			p_script = Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE); 
			m_script = p_script.matcher(htmlStr); 
			htmlStr = m_script.replaceAll(""); //过滤script标签 

			p_style = Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE); 
			m_style = p_style.matcher(htmlStr); 
			htmlStr = m_style.replaceAll(""); //过滤style标签 
      
			p_html = Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE); 
			m_html = p_html.matcher(htmlStr); 
			htmlStr = m_html.replaceAll(""); //过滤html标签 
			
			p_kong = Pattern.compile(kong,Pattern.CASE_INSENSITIVE); 
			m_kong = p_kong.matcher(htmlStr); 
			htmlStr = m_kong.replaceAll(""); //过滤html标签 
      
			textStr = htmlStr; 
      
		}catch(Exception e) { 
			e.printStackTrace();
		} 
		return textStr ;
	}
	
	//discuss分页
	public JsonVo<Udiscusser> getPagination(Integer key){
		//int pagenum是当前的页码,int pagesize是每页显示的数据数量//一定要在setPageCount()方法之后执行setPageNum
		JsonVo<Udiscusser> json = new JsonVo<Udiscusser>();
		json.setT(UdiscusserMapper.selectByDis(key));
		return json;
	}
	
	//seeone 后台
	public Udiscusser selectByPrimaryOn(Integer id,Integer pageNum,Integer pagesize) {
		Udiscusser u = UdiscusserMapper.selectByPrimaryKey(id);
		Integer page =0 ;
		Integer count = UdiscussMapper.selectCountBydrId(id);
		if(count==0){
			page = 1;
		}else if(count%pagesize==0){
			page = count/pagesize;
		}else{
			page = count/pagesize+1;
		}
		if(pageNum<=0){
			pageNum = 1;
		}
		if(pageNum >= page ){
			pageNum = page ;
		}
		System.out.println((pageNum-1)*pagesize);
		List<Udiscuss> us = UdiscussMapper.selectBydrId(id,(pageNum-1)*pagesize,pagesize);
		for(Udiscuss ur:us){
			ur.setDsContent(ur.getDsContent());
			ur.setUsers(UsersMapper.selectByPrimaryKey(ur.getUsersUId()));
			ur.setSuport(UsupportMapper.selectCountBydsId(ur.getDsId()));
		}
		u.setDiscuss(us);
		return u;
	}
	
	
	//discusser 最新
	public PaginationVo<Udiscusser> getByNow(String key,int pagesize,int pagenum){
		//int pagenum是当前的页码,int pagesize是每页显示的数据数量
		PaginationVo<Udiscusser> pageVo=new PaginationVo<Udiscusser>();
		//一定要在setPageCount()方法之后执行setPageNum
		List<Udiscusser> list = null;
		if(key ==null || key ==""){
			pageVo.setCount(UdiscusserMapper.selectCount());
			pageVo.setRows(pagesize);
			pageVo.setPageNum(pagenum);
			list = UdiscusserMapper.selectByNow(pageVo.getOffset(), pageVo.getRows());
		}else{
			pageVo.setCount(UdiscusserMapper.selectCountByKey(key));
			pageVo.setRows(pagesize);
			pageVo.setPageNum(pagenum);
			System.out.println("aaaaaaaaaa"+UdiscusserMapper.selectCountByKey(key));
			list = UdiscusserMapper.selectByKey(key,pageVo.getOffset(), pageVo.getRows());
		}
		for(Udiscusser one:list){
			one.setAnswers(UdiscussMapper.selectCountBydrId(one.getDrId()));
			one.setUsers(UsersMapper.selectByPrimaryKey(one.getUsersUId()));
			String n = getNOHTML(one.getDrContent());
			if(n.length()>80){
				n = n.substring(0, 80)+"..." ;
			}
			one.setDrContent(n);
		}
		pageVo.setList(list);
		return pageVo;
	}
	
	//discusser 查询我参与的
	public PaginationVo<Udiscusser> getByCanyu(int uId,int pagenum,int pagesize){
		//int pagenum是当前的页码,int pagesize是每页显示的数据数量
		PaginationVo<Udiscusser> pageVo=new PaginationVo<Udiscusser>();
		//一定要在setPageCount()方法之后执行setPageNum
		pageVo.setCount(UdiscusserMapper.selectCountByCanyu(uId));
		pageVo.setRows(pagesize);
		pageVo.setPageNum(pagenum);
		List<Udiscusser> list = UdiscusserMapper.selectByCanyu(uId,pageVo.getOffset(), pageVo.getRows());
		for(Udiscusser one:list){
			one.setAnswers(UdiscussMapper.selectCountBydrId(one.getDrId()));
			one.setUsers(UsersMapper.selectByPrimaryKey(one.getUsersUId()));
			String n = getNOHTML(one.getDrContent());
			if(n.length()>80){
				n = n.substring(0, 80)+"..." ;
			}
			one.setDrContent(n);
		}
		pageVo.setList(list);
		return pageVo;
	}
	
	//线下交流
	public PaginationVo<Udiscusser> getByXianxia(Integer type,int pagesize,int pagenum){
		//int pagenum是当前的页码,int pagesize是每页显示的数据数量
		PaginationVo<Udiscusser> pageVo=new PaginationVo<Udiscusser>();
		//一定要在setPageCount()方法之后执行setPageNum
		pageVo.setCount(UdiscusserMapper.selectCountByCanyu(type));
		pageVo.setRows(pagesize);
		pageVo.setPageNum(pagenum);
		System.out.println(pageVo.getOffset()+"_aaaaaa_"+pageVo.getRows()+"_aaaaaaaaa_"+pageVo.getPageCount());
		List<Udiscusser> list = UdiscusserMapper.selectByXianxia(type,pageVo.getOffset(), pageVo.getRows());
		for(Udiscusser one:list){
			String m = one.getDrContent();
			String n = m.substring(m.indexOf("标题")+3,m.indexOf("</p>"));
			one.setDrContent(n);
			one.setAnswers(UdiscussMapper.selectCountBydrId(one.getDrId()));
			one.setUsers(UsersMapper.selectByPrimaryKey(one.getUsersUId()));
		}
		pageVo.setList(list);
		return pageVo;
	}
	
	
	
	
}
